/*
 * @Author: andy.dell andy@long.com
 * @Date: 2024-01-07 21:37:44
 * @LastEditors: andy.dell andy@long.com
 * @LastEditTime: 2024-01-07 21:37:44
 * @FilePath: \wasm\logic\snake\src\model\snake.go
 * @Description:
 */
package model

import (
	"com.game.snake/consts"
	"com.game.snake/utils"
)

type Snake struct {
	Body []int       // 索引列表
	Dict map[int]Pos // index: [row,col]
}
type Pos struct {
	Row int
	Col int
}

func NewSnake(height, width int) *Snake {
	row := height / 2
	col := width / 2
	var body []int                             // 切片 默认值非nil
	dict := make(map[int]Pos, consts.SnakeLen) // map默认值为nil
	for c := row; c > col-consts.SnakeLen; c-- {
		index := utils.Pos2Index(width, row, c)
		dict[index] = Pos{row, c}
		body = append(body, index)
	}
	return &Snake{
		Dict: dict,
		Body: body,
	}
}
func (s *Snake) GetHeadPos() Pos {
	return s.Dict[s.Body[0]]
}
